package com.tpsix.configure;

import com.baomidou.mybatisplus.extension.plugins.PaginationInterceptor;
import com.baomidou.mybatisplus.extension.plugins.pagination.optimize.JsqlParserCountOptimize;
import com.baomidou.mybatisplus.extension.plugins.tenant.TenantHandler;
import com.baomidou.mybatisplus.extension.plugins.tenant.TenantSqlParser;
import lombok.AllArgsConstructor;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.transaction.annotation.EnableTransactionManagement;

import java.util.Collections;

@Configuration
@AllArgsConstructor
@EnableTransactionManagement
public class MybatisPlusConfiguration {

    private final TenantHandler tenantHandler;

    @Bean
    public PaginationInterceptor paginationInterceptor() {

        // 开启 count 的 join 优化,只针对 left join !!!
        PaginationInterceptor paginationInterceptor = new PaginationInterceptor();
        paginationInterceptor.setCountSqlParser(new JsqlParserCountOptimize(true));

        // 多租户解析
        TenantSqlParser sqlParser = new TenantSqlParser();
        sqlParser.setTenantHandler(tenantHandler);
        paginationInterceptor.setSqlParserList(Collections.singletonList(sqlParser));

        return paginationInterceptor;
    }

}

